/*
 * yocef - Lightweight IoC Event Framework
 *
 * Copyright (c) 2009, Yves Cuillerdier
 * All rights reserved.
 * 
 * This software is licensed under the new BSD License:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * @version 1.0.2
 */
package yocef.client;

import java.util.Map;
import java.util.TreeMap;

/**
 * An EventContext is a set of named elements that an event handler need to know
 * about in order to handle or process the event.
 * <p>
 * An EventContext could be shared by many events.
 * <p>
 * Example :
 * <code>new EventContext().add("okButton",button).add("textField1",tField1).add(..)</code>
 */
public class EventContext {

	/* The objects in the context */
	private final Map<String, Object> context = new TreeMap<String, Object>();

	/**
	 * Empty constructor.
	 */
	public EventContext() {
		super();
	}

	/**
	 * Add a new element to be referred by the given name in the context.
	 * 
	 * @param key
	 *            the key name used to refers to the element
	 * @param element
	 *            the element to be referred
	 * @return this to allow "chaining"
	 */
	public EventContext put(final String key, final Object element) {
		// Set value
		context.put(key, element);
		return this;
	}

	/**
	 * Get the context element referred by the given key name.
	 * 
	 * @param key
	 *            the key name used to refers to the element
	 * @return the element if it exists or null
	 */
	public Object get(final String key) {
		return context.get(key);
	}

	/**
	 * Remove the element referred to by the given key name.
	 * 
	 * @param key
	 *            the key name used to refers to the element
	 */
	public void remove(final String key) {
		context.remove(key);
	}

	/**
	 * get a String representation for this EventContext.
	 * 
	 * @return an EventContext String representation.
	 */
	@SuppressWarnings("nls")
	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("[ ");
		boolean first = true;
		for (final String key : context.keySet()) {
			if (!first) {
				sb.append(", ");
			} else {
				first = false;
			}
			sb.append("{ ");
			sb.append(key);
			sb.append(": \"");
			sb.append(context.get(key));
			sb.append("\" }");
		}
		sb.append(" ]");
		return sb.toString();
	}
}
